<%@ page import="dao.EquipmentDAO" %>
<%@ page import="entity.EquipmentBean" %>
<%@ page import="java.util.List" %>
<%@ page import="entity.Page" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <link rel="shortcut icon" href="#"/>
    <link rel="stylesheet" type="text/css" href="../css/equipment_list.css">
</head>
</head>
<body>
<%
    String eq_data = request.getParameter("eq_data");
    if(eq_data == null){
        eq_data = "";
    }
    String eq_number = request.getParameter("eq_number");
    if(eq_number == null){
        eq_number = "";
    }
    String eq_type = request.getParameter("eq_type");
    if(eq_type == null){
        eq_type = "";
    }
    String eq_producer = request.getParameter("eq_producer");
    if(eq_producer == null){
        eq_producer = "";
    }
    String eq_after_sale = request.getParameter("eq_after_sale");
    if(eq_after_sale == null){
        eq_after_sale = "";
    }
%>
<div>
    <div id="headDiv">
        <div id="logo">
            <img src="../img/logo_search.png" width="30" height="30" alt=""/>
        </div>
        <div id="title">设备查询</div>
    </div>
    <form action="<%=request.getContextPath()%>/group/equipment/Equipment_list" method="post">
        <div id="searchDiv">
        设备信息 <input class="searchBorder" type="text" placeholder="请输入设备编号" name="eq_number" id="eq_number" value="<%=eq_number%>">
        设备类型 <input class="searchBorder" type="text" placeholder="类型" name="eq_type" id="eq_type" value="<%=eq_type%>">
        供应商名称 <input class="searchBorder" type="text" placeholder="请输入供应商名称" name="eq_producer" id="eq_producer" value="<%=eq_producer%>">
        售后联系人: <input class="searchBorder" type="text" placeholder="请输入售后联系人" name="eq_after_sale" id="eq_after_sale" value="<%=eq_after_sale%>">
        日期: <input class="searchBorder" type="date" placeholder="请输入生产日期" name="eq_data" id="eq_data" value="<%=eq_data%>">
        <input class="bkg1" type="submit" value="查询">
        <input class="bkg2" type="reset" value="重置">
        <button class="bkg3" type="button"><a href="<%=request.getContextPath()%>/group/equipment/Equipment_edit?id=0">新增</a></button>
        </div>
    </form>
    <table border="1" cellspacing="0" cellpadding="0">
        <thead>
        <tr>
            <th>序号</th>
            <th>设备名称</th>
            <th>设备编号</th>
            <th>设备类型</th>
            <th>供应商名称</th>
            <th>出厂日期</th>
            <th>质保期</th>
            <th>售后联系人</th>
            <th>联系电话</th>
            <th>质保说明</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <%
            EquipmentDAO eq_list = new EquipmentDAO();
            List<EquipmentBean> eq_List = null;
            eq_List = (List<EquipmentBean>) request.getAttribute("eq_list");
            if(eq_List !=null){
                for (EquipmentBean obj: eq_List) {
        %>
        <tr>
            <td><%= obj.getEq_id()%></td>
            <td><%= obj.getEq_name()%></td>
            <td><%= obj.getEq_number()%></td>
            <td><%= obj.getEq_type()%></td>
            <td><%= obj.getEq_producer()%></td>
            <td><%= obj.getEq_data()%></td>
            <td><%= obj.getEq_store_data()%></td>
            <td><%= obj.getEq_after_sale()%></td>
            <td><%= obj.getEq_phone()%></td>
            <td><%= obj.getEq_introduction()%></td>
            <td>
                <button class="button1" onclick="editRecord(<%=obj.getEq_id()%>)">编辑</button>
                <button class="button2" onclick="deleteRecord(<%=obj.getEq_id()%>)">删除</button>
            </td>
        </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
    <div class="pagination">
        <%
            Page equipmentPage=(Page)request.getAttribute("equipmentPage");
            if(equipmentPage!=null){
        %>
        <% if(equipmentPage.getCurrentPage() > 1) { %>
        <button class="prev" onclick="goToPrevPage()">&laquo; 上一页</button>
        <% } %>

        <% for(int i = Math.max(1, equipmentPage.getCurrentPage() - 4); i <= Math.min(equipmentPage.getCurrentPage() + 4, equipmentPage.getTotalPage()); i++) { %>
        <button onclick="goToNum(<%= i %>)" <% if(i == equipmentPage.getCurrentPage()) { %>class="active"<% } %>><%= i %></button>
        <% } %>


        <% if(equipmentPage.getCurrentPage() < equipmentPage.getTotalPage()) { %>
        <button class="next" onclick="goToNextPage()">下一页 &raquo;</button>
        <% } %>

        <button onclick="goToFirst()">首页</button>
        <button onclick="goToLast()">尾页</button>
        <%}%>
    </div>
</div>
</body>
</html>
<script>
    function goToNum(i){

        <%if(equipmentPage!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + i+"&eq_number="+document.getElementById("eq_number").value+"&eq_type="+document.getElementById("eq_type").value+"&eq_producer="+document.getElementById("eq_producer").value+"&eq_after_sale="+document.getElementById("eq_after_sale").value+"&eq_data="+document.getElementById("eq_data").value;

        <%}%>

    }
    function goToFirst(){
        <%if(equipmentPage!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        var nextPage = <%=1  %>;

        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + nextPage +"&eq_number="+document.getElementById("eq_number").value+"&eq_type="+document.getElementById("eq_type").value+"&eq_producer="+document.getElementById("eq_producer").value+"&eq_after_sale="+document.getElementById("eq_after_sale").value+"&eq_data="+document.getElementById("eq_data").value;

        <%}%>

    }

    function goToLast(){
        <%if(equipmentPage!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        var nextPage = <%=equipmentPage.getTotalPage()  %>;

        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + nextPage +"&eq_number="+document.getElementById("eq_number").value+"&eq_type="+document.getElementById("eq_type").value+"&eq_producer="+document.getElementById("eq_producer").value+"&eq_after_sale="+document.getElementById("eq_after_sale").value+"&eq_data="+document.getElementById("eq_data").value;

        <%}%>

    }
    function goToNextPage() {
        <%if(equipmentPage!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        var nextPage = <%= equipmentPage.getCurrentPage()+1  %>;

        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + nextPage +"&eq_number="+document.getElementById("eq_number").value+"&eq_type="+document.getElementById("eq_type").value+"&eq_producer="+document.getElementById("eq_producer").value+"&eq_after_sale="+document.getElementById("eq_after_sale").value+"&eq_data="+document.getElementById("eq_data").value;

        <%}%>
    }

    function goToPrevPage(){
        <%if(equipmentPage!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        var nextPage = <%= equipmentPage.getCurrentPage()-1  %>;

        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + nextPage +"&eq_number="+document.getElementById("eq_number").value+"&eq_type="+document.getElementById("eq_type").value+"&eq_producer="+document.getElementById("eq_producer").value+"&eq_after_sale="+document.getElementById("eq_after_sale").value+"&eq_data="+document.getElementById("eq_data").value;

        <%}%>
    }
    function deleteRecord(id) {
        if (confirm("确定要删除这条记录吗？")) {
            // 发送删除请求到服务器
            window.location.href="<%=request.getContextPath()%>/group/equipment/Equipment_delete?id=" + id;
        }
    }
    function editRecord(id) {
        if (confirm("确定要编辑这条记录吗？")) {
            // 发送删除请求到服务器
            window.location.href="<%=request.getContextPath()%>/group/equipment/Equipment_edit?id=" + id;
        }
    }

</script>